﻿/// <reference path="../../release/jsApp.js"/>

/* Base Controller for all UiControllers */

function UiController(app, tagId)
{
	UiController.$baseConstructor.call(this);

	this.app = app;
	this.tagId = tagId;
	this.path = null;
	this.prevPath = null;
	this.nextPath = null;
	this.controllers = [];

//	UiController.registerControllers(UiController, [this.constructor]);
}
UiController.extend(ASObject, { type: 'AjaxStack.UiController' },
	function(p)
	{
		p.getApp = function() { return this.app; }

		p.getPath = function() { return this.path; }

		p.setNavPath = function(prevPath, nextPath)
		{
			this.prevPath = prevPath;
			this.nextPath = nextPath;
		}

		p.getTagId = function() { return this.tagId; }

		p.getTitle = function() { return "Untitled"; }

		p.getBreadcrumbs = function() { return []; }

		p.getName = function()
		{
			return this.constructor.getName
				? this.constructor.getName() : this.constructor.getType();
		}

		p.getControllers = function()
		{
			var all = [];
			for (var i = 0, len = this.controllers.length; i < len; i++)
			{
				var controller = this.controllers[i];
				all.push(controller);
				A.merge(all, controller.getControllers());
			}
			return all;
		}

		p.onAfterRender = function() { }

		p.render = function(html, tagId)
		{
			tagId = tagId || this.getTagId();
			if (!tagId) return;
			$(tagId).html(html);

			this.onAfterRender();
			this.dispatchEvent("onAfterContentLoaded", this, { id: tagId });
		}

		p.dispatchEvent = function(eventName, source, args)
		{
			this.getApp().dispatchEvent(eventName, source, args);
		}

		p.loadPath = function(path)
		{
			if (!path) return;
			this.path = path;

			if (AppBase.getAppPages())
			{
				this.log.warn("AppPages not set, skipping.");

				var pageFn = AppBase.getAppPages().pages[this.path.getName()];
				if (pageFn)
				{
					this.log.debug("Rendering html for path: " + this.path.getName());
					this.render(pageFn());
					return;
				}
			}
			this.log.debug("Could not find html for path: " + this.path.getName());
			this.render();
		}
	}
);
UiController.controllerMap = {};
UiController.log = new Logger(UiController.getType());
UiController.getController = function(name)
{
	return this.controllerMap[name];
}
UiController.registerControllers = function(masterCtor, controllers, options)
{
	if (!masterCtor.controllerMap)
	{
		masterCtor.controllerMap = {};
		masterCtor.getController = UiController.getController;
	}
	for (var i = 0, len = controllers.length; i < len; i++)
	{
		var c = controllers[i];
		var name = c.getName ? c.getName() : c.getType();
		masterCtor.controllerMap[name] = c;
	}

	options = options || {};
	masterCtor.canHandlePath = options.canHandlePathFn || UiController.handleRegisteredPaths;
}

//return not-null regardless of name to indicate it can handle all paths
UiController.wildcard = new UiController();
UiController.handleRegisteredPaths = function(path)
{
	return this.getController(path.getName()) != null;
}
UiController.handleAnyPath = function(path)
{
	return true;
}
UiController.render = function(tagId, html)
{
	if (!tagId) return;
	$(tagId).html(html);
	AppBase.getInstance().dispatchEvent("onAfterContentLoaded", this, { id: tagId });
}